<?php

namespace App\Logic\Account;

use App\Component\LogListForm;
use App\Core\BaseCore;
use App\Core\BaseForm;
use App\Core\BaseListForm;
use App\Core\BaseModel;
use App\Core\Exception\DefaultException;
use App\Core\Exception\ValidationException;
use App\Core\Output;
use App\Enums\ValidateEnums;
use App\Logic\Account\Form\CancelCashoutForm;
use App\Logic\Account\Form\CashoutListForm;
use App\Logic\Account\Form\FinshCashoutForm;
use App\Logic\Account\Form\SubmitCashoutProfitForm;
use App\Logic\Member\MemberService;
use App\Logic\Purchase\PurchaseService;
use App\Model\Account\CashoutModel;
use App\Logic\Account\Form\SubmitCashoutForm;
use App\Model\Account\RechargeModel;
use App\Model\Account\StatisticsModel;
use App\Model\Member\MemberModel;
use App\Util\ArrayHelper;
use App\Util\CommonHelper;
use App\Util\UserIdentityHelper;
use EasySwoole\Component\Di;
use EasySwoole\Component\Singleton;
use EasySwoole\EasySwoole\Config as ESConfig;
use EasySwoole\Http\Request;
use EasySwoole\Log\Logger;
use EasySwoole\ORM\DbManager;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\Redis\Redis;
use EasySwoole\RedisPool\RedisPool;
use EasySwoole\Utility\Random;
use EasySwoole\Validate\Validate;
use Throwable;

class StatisticsService extends BaseCore
{

    use Singleton;

    /**
     * 字典数据
     * @param Request $request
     * @return Output
     * @throws Exception
     * @throws Throwable
     */
    public function getDict(Request $request) : Output
    {
        $params = $request->getQueryParams();
        $result = [
        ];
        return $this->success($result);
    }

    /**当前有效的数据*/
    public function getCurrentStatistics() : Output
    {
        if($data = StatisticsModel::create()->getOne([])){
            $data = $data->toArray();
        }else{
            $data = [
                'count_member' => 0,
                'total_score' => 0,
                'rate' => 0,
                'available_score' => 0,
                'count_purchase' => 0,
                'purchase_amount' => 0,
                'count_unshipping_purchase' => 0,
                'unshipping_purchase_amount' => 0,
                'count_shipped_purchase' => 0,
                'shipped_purchase_amount' => 0,
                'count_receipt_purchase' => 0,
                'receipt_purchase_amount' => 0,
            ];
        }
        return $this->success($data);
    }

    /**统计账户和订单数据*/
    public function jobStatisticsAll()
    {
        \EasySwoole\EasySwoole\Logger::getInstance()->info('统计账户和订单数据');
        $data = [];
        $memberData = MemberService::getInstance()->countUserNumAndScore();
        $data['count_member'] = $memberData['count'];
        $data['total_score'] = $memberData['score'];
        $orderData = PurchaseService::getInstance()->countOrder();
        $data['count_purchase'] = $orderData['count_purchase'];
        $data['purchase_amount'] = $orderData['purchase_amount'];
        $data['count_unshipping_purchase'] = $orderData['count_unshipping_purchase'];
        $data['unshipping_purchase_amount'] = $orderData['unshipping_purchase_amount'];
        $data['count_shipped_purchase'] = $orderData['count_shipped_purchase'];
        $data['shipped_purchase_amount'] = $orderData['shipped_purchase_amount'];
        $data['count_receipt_purchase'] = $orderData['count_receipt_purchase'];
        $data['receipt_purchase_amount'] = $orderData['receipt_purchase_amount'];
        $data['available_score'] = bcsub(bcmul($memberData['score'], StatisticsModel::RATE, 2), bcadd($orderData['unshipping_purchase_amount'], $orderData['shipped_purchase_amount'], 2), 2);
        $data['rate'] = StatisticsModel::RATE;
        DbManager::getInstance()->startTransactionWithCount('write');
        try {
            StatisticsModel::create()->deleteRecord([]);
            $res = StatisticsModel::create()->insertData($data);
            if (!$res) {
                throw new DefaultException('统计数据失败');
            }
            DbManager::getInstance()->commitWithCount('write');
        } catch (Throwable $e) {
            DbManager::getInstance()->rollbackWithCount('write');
            \EasySwoole\EasySwoole\Logger::getInstance()->error($e->getMessage());
        }
        return true;
    }

}
